package com.infotel.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JFileChooser;

public class CopierFichier {

    /**
     * @param args
     */
    public static void main(String[] args) {
        InputStream is = null;
        OutputStream os = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        byte[] buffer = new byte[2048];

        String cheminDestination = "C:\\a_workspace\\Multimedia\\plop.jpg";

        try {
            URL url = new URL(
                    "http://www.infotel.com/wp-content/uploads/2013/03/slider-home.jpg");

            new JFileChooser();
            new JFileChooser();

            // int returnVal = chooserSource.showOpenDialog(null);
            // if (returnVal == JFileChooser.APPROVE_OPTION) {
            // cheminSource = chooserSource.getSelectedFile().getName();
            // }
            // returnVal = chooser.showOpenDialog(null);
            // if (returnVal == JFileChooser.APPROVE_OPTION) {
            // cheminDestination = chooser.getSelectedFile().getName();
            // }
            int nb;
            try {
                // is = new FileInputStream(new File(
                // "C:\\a_workspace\\Multimedia\\files\\Notes\\do.au"));
                // os = new FileOutputStream(
                // "C:\\a_workspace\\Multimedia\\files\\Notes\\do3.au");

                is = url.openStream();

                // is = new FileInputStream(chooserSource.getSelectedFile());
                os = new FileOutputStream(cheminDestination);
                bis = new BufferedInputStream(is);
                bos = new BufferedOutputStream(os);
                try {
                    do {
                        nb = bis.read(buffer);
                        if (nb >= 0) {
                            bos.write(buffer, 0, nb);
                            bos.flush();
                        }

                    } while (nb != -1);
                    /*
                     * do { nb = is.read(); os.write(nb); } while (nb != -1);
                     */

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

}
